package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/TravelItem")
public class TravelItemController {

    @Reference
    TravelItemService travelItemService;


    @RequestMapping("/getUaerName")
    public Result getUserName(){
        try {
            User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true,"登录成功",principal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户名失败");
        }
    }

    /**
     * 新增功能
     * @param travelItem
     * @return
     */
    @PreAuthorize("hasAuthority('TRAVELITEM_ADD1')")
    @PostMapping("/add")
    public Result add(@RequestBody TravelItem travelItem){

            try {
                travelItemService.add(travelItem);
                return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
           }
    }

    /**
     * 分页查询功能
     * @param
     * @return
     */
    @PostMapping("/select")
    public PageResult select(@RequestBody QueryPageBean param){
        PageResult pageResult = travelItemService.select(param.getCurrentPage(),param.getPageSize(),param.getQueryString());
        return pageResult;
    }

    /**
     * 实现删除单条数据功能
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id){

        try {
            int i = travelItemService.getRelevance(id);
            if(i > 0){
                return new Result(false,"该数据存在关联，请先输出关联的组");
            }else {
                travelItemService.delete(id);
                return new Result(true, MessageConstant.DELETE_TRAVELITEM_SUCCESS);
            }
            } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/getById")
    public Result getById(Integer id){
        try {

                TravelItem travelItem = travelItemService.getById(id);
                return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItem);


        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }


    @PostMapping("/update")
    public Result update(@RequestBody TravelItem travelItem){
        try {
            travelItemService.update(travelItem);
            return new Result(true,MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }

}
